import { create } from 'zustand';
import { persist } from 'zustand/middleware';

export interface AuthStore {
  userToken: string | null;
  isLogged: () => boolean;
  setToken: (newToken: AuthStore['userToken']) => void;
  logout: () => void;
}

const useAuthStore = create<AuthStore>()(
  persist(
    (set, get) => ({
      userToken: null,
      isLogged: () => get().userToken !== null,
      setToken: newToken =>
        set({
          userToken: newToken,
        }),
      logout: () =>
        set({
          userToken: null,
        }),
    }),
    {
      name: 'app-auth-store',
    },
  ),
);

export default useAuthStore;
